.NET6 C#, LineBot, Line Messaging API, C#, dotnet core
Hihi 各位好,上一篇我們建立了 6 種的 webhook event,這一篇將會再介紹 常用的 4 種 events,其實總共不是有 15 種 events 嗎 ? 這樣也才介紹 10 種欸 ?
沒錯,剩下的 5 種事件不容易展示,一般人碰到的機會應該也會太多,所以本系列就先略過,有興趣的人 再留言讓我知道囉。
public class MemberEventDto
{
public List<SourceDto> Members { get; set; }
}
public class WebhookEventDto
{
// -------- 以下 common property --------
.
.
.
// -------- 以下 event properties--------
public string? ReplyToken { get; set; } // 回覆此事件所使用的 token
public MessageEventDto? Message { get; set; } // 收到訊息的事件,可收到 text、sticker、image、file、video、audio、location 訊息
public UnsendEventDto? Unsend { get; set; } //使用者“收回”訊息事件
public MemberEventDto? Joined { get; set; } // Memmber Joined Event
public MemberEventDto? Left { get; set; } // Member Leave Event
}
Postback 會是一個很常用到的事件,在設計 Line Bot 互動內容時,會有一個 action 叫做 Postback action,可以在 postback.data 的屬性中帶任意字串,點擊觸發後 Line 就會產生此 Postback 事件並傳給我們的伺服器。
官方文件內容
在 WebhookEventsDto.cs 中宣告以下兩個 Class
public class PostbackEventDto
{
public string? Data { get; set; }
public PostbackEventParamDto? Params { get; set; }
}
public class PostbackEventParamDto
{
public string? Date { get; set; }
public string? Time { get; set; }
public string? Datetime { get; set; }
public string? NewRichMenuAliasId { get; set; }
public string? Status { get; set; }
}
public class WebhookEventDto
{
// -------- 以下 common property --------
.
.
.
// -------- 以下 event properties--------
public MessageEventDto? Message { get; set; } // 收到訊息的事件,可收到 text、sticker、image、file、video、audio、location 訊息
public UnsendEventDto? Unsend { get; set; } //使用者“收回”訊息事件
public string? ReplyToken { get; set; } // 回覆此事件所使用的 token
public MemberEventDto? Joined { get; set; } // Memmber Joined Event
public MemberEventDto? Left { get; set; } // Member Leave Event
public PostbackEventDto? Postback { get; set; } // Postback Event
}
後面會介紹傳送影片訊息的方式,而在傳送影片訊息時可以設定其 trackingId 的屬性,當好友看完這部設定過的影片時,就會產生此事件回傳給我們伺服器,這邊就先建好這個事件的 class 後面帶到影片訊息時再一起測試~。
官方文件
在 WebhookEventsDto.cs 中宣告以下 Class
public class VideoViewingCompleteEventObjectDto
{
public string TrackingId { get; set; }
}
public class WebhookEventDto
{
// -------- 以下 common property --------
.
.
.
// -------- 以下 event properties--------
public MessageEventDto? Message { get; set; } // 收到訊息的事件,可收到 text、sticker、image、file、video、audio、location 訊息
public UnsendEventDto? Unsend { get; set; } //使用者“收回”訊息事件
public string? ReplyToken { get; set; } // 回覆此事件所使用的 token
public MemberEventDto? Joined { get; set; } // Memmber Joined Event
public MemberEventDto? Left { get; set; } // Member Leave Event
public PostbackEventDto? Postback { get; set; } // Postback Event
public VideoViewingCompleteEventDto? VideoPlayComplete { get; set; } // Video viewing complete event
}
namespace LineBotMessage.Dtos
{
public class WebhookEventDto
{
// -------- 以下 common property --------
public string Type { get; set; } // 事件類型
public string Mode { get; set; } // Channel state : active | standby
public long Timestamp { get; set; } // 事件發生時間 : event occurred time in milliseconds
public SourceDto Source { get; set; } // 事件來源 : user | group chat | multi-person chat
public string WebhookEventId { get; set; } // webhook event id - ULID format
public DeliverycontextDto DeliveryContext { get; set; } // 是否為重新傳送之事件 DeliveryContext.IsRedelivery : true | false
// -------- 以下 event properties--------
public MessageEventDto? Message { get; set; } // 收到訊息的事件,可收到 text、sticker、image、file、video、audio、location 訊息
public UnsendEventDto? Unsend { get; set; } //使用者“收回”訊息事件
public string? ReplyToken { get; set; } // 回覆此事件所使用的 token
public MemberEventDto? Joined { get; set; } // Memmber Joined Event
public MemberEventDto? Left { get; set; } // Member Leave Event
public PostbackEventDto? Postback { get; set; } // Postback Event
public VideoViewingCompleteEventDto? VideoPlayComplete { get; set; } // Video viewing complete event
}
// -------- 以下 common property --------
public class SourceDto
{
public string Type { get; set; }
public string? UserId { get; set; }
public string? GroupId { get; set; }
public string? RoomId { get; set; }
}
public class DeliverycontextDto
{
public bool IsRedelivery { get; set; }
}
// -------- 以下 message event --------
public class MessageEventDto
{
public string Id { get; set; }
public string Type { get; set; }
// Text Message Event
public string? Text { get; set; }
public List<TextMessageEventEmojiDto>? Emojis { get; set; }
public TextMessageEventMentionDto? Mention { get; set; }
// Image & Video & Audio Message Event
public ContentProviderDto? ContentProvider { get; set; }
public ImageMessageEventImageSetDto? ImageSet { get; set; }
public int? Duration { get; set; }
//File Message Event
public string? FileName { get; set; }
public int? FileSize { get; set; }
//Location Message Event
public string? Title { get; set; }
public string? Address { get; set; }
public double? Latitude { get; set; }
public double? Longitude { get; set; }
// Sticker Message Event
public string? PackageId { get; set; }
public string? StickerId { get; set; }
public string? StickerResourceType { get; set; }
public List<string>? Keywords { get; set; }
}
public class TextMessageEventEmojiDto
{
public int Index { get; set; }
public int Length { get; set; }
public string ProductId { get; set; }
public string EmojiId { get; set; }
}
public class TextMessageEventMentionDto
{
public List<TextMessageEventMentioneeDto> Mentionees { get; set; }
}
public class TextMessageEventMentioneeDto
{
public int Index { get; set; }
public int Length { get; set; }
public string UserId { get; set; }
}
public class ContentProviderDto
{
public string Type { get; set; }
public string? OriginalContentUrl { get; set; }
public string? PreviewImageUrl { get; set; }
}
public class ImageMessageEventImageSetDto
{
public string Id { get; set; }
public string Index { get; set; }
public string Total { get; set; }
}
// -------- 以下 unsend event --------
public class UnsendEventDto
{
public string messageId { get; set; }
}
// -------- 以下 member joined & left event --------
public class MemberEventDto
{
public List<SourceDto> Members { get; set; }
}
// -------- 以下 postback event --------
public class PostbackEventDto
{
public string? Data { get; set; }
public PostbackEventParamDto? Params { get; set; }
}
public class PostbackEventParamDto
{
public string? Date { get; set; }
public string? Time { get; set; }
public string? Datetime { get; set; }
public string? NewRichMenuAliasId { get; set; }
public string? Status { get; set; }
}
// -------- 以下 videoViewingCompleteEventDto event --------
public class VideoViewingCompleteEventDto
{
public string TrackingId { get; set; }
}
}
public void ReceiveWebhook(WebhookRequestBodyDto requestBody)
{
foreach(var eventObject in requestBody.Events)
{
switch (eventObject.Type)
{
.
.
.
case WebhookEventTypeEnum.Leave:
Console.WriteLine("我們被聊天室踢出了");
break;
// -------- 新增內容 --------
case WebhookEventTypeEnum.MemberJoined:
string joinedMemberIds = "";
foreach(var member in eventObject.Joined.Members)
{
joinedMemberIds += $"{member.UserId} ";
}
Console.WriteLine($"使用者{joinedMemberIds}加入了群組!");
break;
case WebhookEventTypeEnum.MemberLeft:
string leftMemberIds = "";
foreach (var member in eventObject.Left.Members)
{
leftMemberIds += $"{member.UserId} ";
}
Console.WriteLine($"使用者{leftMemberIds}離開了群組!");
break;
case WebhookEventTypeEnum.Postback:
Console.WriteLine($"使用者{eventObject.Source.UserId}觸發了postback事件");
break;
case WebhookEventTypeEnum.VideoPlayComplete:
Console.WriteLine($"使用者{eventObject.Source.UserId}");
break;
// -------- 新增內容 --------
}
}
}
Webhook event 的介紹就到這邊結束了,下一篇進入訊息的介紹與推播API的串接,會是一個比較重要的部分,訊息推播介紹之後就可以將這兩篇介紹的事件接收處理改成用訊息回覆的方式跟使用者直接的互動~就這樣,下一篇見囉!
如果想要參考今天範例程式碼的部份,下面是 Git Repo 連結,方便大家參考。